package com.hanrui.android.net.presenter;


import android.content.Context;
import android.util.Log;

import com.hanrui.android.activity.LoginActivity;
import com.hanrui.android.library.base.BaseBean;
import com.hanrui.android.library.base.BasePresenter;
import com.hanrui.android.constant.Constant;
import com.hanrui.android.library.net.ApiService;
import com.hanrui.android.library.net.RetrofitManager;
import com.hanrui.android.library.net.RxSchedulers;
import com.hanrui.android.net.contract.MainContract;

import javax.inject.Inject;

import io.reactivex.functions.Consumer;

/**
 * 主页
 * Created by menglei on 2018/3/22.
 */

public class MainPresenter extends BasePresenter<MainContract.View> implements MainContract.Presenter {

    @Inject
    public MainPresenter() {
    }

    @Override
    public void loginRegist(final Context context, String uuid) {

        mView.showLoading();
        RetrofitManager.create(ApiService.class, Constant.baseUrl)
                .loginRegist(uuid)
                .compose(RxSchedulers.<BaseBean>applySchedulers())
                .compose(mView.<BaseBean>bindToLife())
                .subscribe(new Consumer<BaseBean>() {
                    @Override
                    public void accept(BaseBean response) throws Exception {
                        Log.e(Constant.tag,response.getCode()+"");
                        mView.hideLoading();
                        if (response.getCode() == 201){
                            LoginActivity.start(context);
                            mView.showFaild("会话过期请重新登录");
                        }else {
                            mView.loginRegist(response.getCode());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.hideLoading();
                        mView.showFaild(throwable.getMessage());
                    }
                });

    }
}
